//
//  CTCollectionView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/7.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTCollectionView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "CTSetView_Def.h"
#import "UIViewAdditions.h"

@interface CTCollectionView()<UITableViewDataSource,UITableViewDelegate>
{
    UIView      *backgroundView;
    UIView      *foregroundView;
    UIImageView * topImageViewbg;
    UITableView *colTableView;
    NSMutableArray     *dataAry;
}
@end
@implementation CTCollectionView
@synthesize delegate;
- (void)dealloc
{
    backgroundView = nil;
    foregroundView = nil;
    topImageViewbg = nil;
    colTableView   = nil;
    dataAry        = nil;
    delegate       = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTShareView Is Removed!!!"];
}
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        dataAry = [NSMutableArray array];
        [self createCollectionView];
    }
    return self;
}

-(void)showShareViewOn:(UIView *)view
{
    if (view == nil) {
        return;
    }
    [view addSubview:self];
    
}
- (void)createCollectionView
{
    //背景
    backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT)];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0.8;
    [self addSubview:backgroundView];
    //前景
    foregroundView = [[UIView alloc]initWithFrame:CGRectMake(0, CT_HEADER_SCREEN_HEIGHT, CT_HEADER_SCREEN_WIDTH, 375)];
    [self addSubview:foregroundView];
    
    topImageViewbg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 50)];
    topImageViewbg.image = [UIImage imageNamed:@"col_bg"];
    [foregroundView addSubview:topImageViewbg];
    
    UILabel *shareLabeLine = [[UILabel alloc]initWithFrame:CGRectMake(0, 49.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    shareLabeLine.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [topImageViewbg addSubview:shareLabeLine];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 50)];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"collectionView_title" withValue:nil withTable:@"Language"];
    titleLabel.textColor =SET_LABEL_TEXT_COLOR;
    titleLabel.font = [UIFont fontWithName:nil size:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [foregroundView addSubview:titleLabel];
    
    UILabel *btnLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 100, 50)];
    btnLabel.text = [CTAppLanguage localizedStringForKey:@"shareView_closeBtn_title" withValue:nil withTable:@"Language"];
    btnLabel.textColor = SET_LABEL_TEXT_COLOR;
    btnLabel.font = [UIFont fontWithName:nil size:18];
    btnLabel.textAlignment = NSTextAlignmentLeft;
    [foregroundView addSubview:btnLabel];
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 60, 50);
    [button addTarget:self action:@selector(closeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:SET_LABEL_TEXT_COLOR forState:UIControlStateNormal];
    [foregroundView addSubview:button];
    
    UILabel *newLabel = [[UILabel alloc]initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH-115, 0, 100, 50)];
    newLabel.text = [CTAppLanguage localizedStringForKey:@"collectionView_newFiel" withValue:nil withTable:@"Language"];
    newLabel.textColor = CT_HEADER_ORANGE_COLOR;
    newLabel.font = [UIFont fontWithName:nil size:18];
    newLabel.textAlignment = NSTextAlignmentRight;
    [foregroundView addSubview:newLabel];
    
    UIButton *newbtn =[UIButton buttonWithType:UIButtonTypeCustom];
    newbtn.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-60, 0, 60, 50);
    [newbtn addTarget:self action:@selector(newBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [newbtn setTitleColor:SET_LABEL_TEXT_COLOR forState:UIControlStateNormal];
    [foregroundView addSubview:newbtn];
    
    
    colTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, topImageViewbg.bottom, CT_HEADER_SCREEN_WIDTH, foregroundView.height-topImageViewbg.height) style:UITableViewStylePlain];
    colTableView.delegate = self;
    colTableView.dataSource = self;
    colTableView.showsVerticalScrollIndicator = NO;
    colTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    colTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    colTableView.backgroundColor = [UIColor whiteColor];
    colTableView.rowHeight = 80.0f;
    [colTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [foregroundView addSubview:colTableView];
    
    [self giveUpAction:@"go" withInt:375];

}
- (void)removeCollectionView
{
    [self removeFromSuperview];
}
//setter
- (void)setArray:(NSArray *)ary
{
    dataAry = [NSMutableArray arrayWithArray:ary];
}
//新建
- (void)newBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(collectionViewNewFile:)])
    {
        [self.delegate collectionViewNewFile:self];
    }
    [colTableView reloadData];
}
//关闭
- (void)closeBtnAction:(UIButton *) sender
{
    [self giveUpAction:nil withInt:375];
}
//弹出收回动画
-(void)giveUpAction:(NSString *)type withInt:(int )int_h
{
    
    if ([type isEqualToString:@"go"])
    {
        [UIView animateWithDuration:0.3f animations:^
         {
             foregroundView.frame = CGRectMake(0, CT_HEADER_SCREEN_HEIGHT-int_h, CT_HEADER_SCREEN_WIDTH, int_h);
             
         }completion:^(BOOL finished){
         }];
    }
    else
    {
        [UIView animateWithDuration:0.3f animations:^
         {
             foregroundView.frame = CGRectMake(0, CT_HEADER_SCREEN_HEIGHT, CT_HEADER_SCREEN_WIDTH, int_h);
             
         }completion:^(BOOL finished)
         {
             [self removeCollectionView];
         }];
    }
}
#pragma mark TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellWithIdentifier = @"Cell";
    CTCollectionViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[CTCollectionViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text = @"萌Mark";
//    cell.headImageView.image =[UIImage imageNamed:@""];
//    cell.timeLabel.text = @"9:00";
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(collectionViewSelect:)])
    {
        [self.delegate collectionViewSelect:self];
    }
}
@end








#import "CTRequestUtil.h"
//#import "CTDebugManager.h"

@implementation CTCollectionViewCell
@synthesize headImageView,nameLabel;
- (void)dealloc
{
    headImageView = nil;
    nameLabel     = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTShareView Is Removed!!!"];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self awakeFromNib];
    }
    
    return self;
}
- (void)awakeFromNib
{
    // Initialization code
    //cell.Height = 120
    [self createFansCell];
}

-(void) createFansCell
{
    headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
    headImageView.image = [UIImage imageNamed:@"peitu_head"];
    headImageView.layer.cornerRadius = 6;
    headImageView.layer.masksToBounds = YES;
    [self addSubview:headImageView];
    
    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(headImageView.right+12, 25, CT_HEADER_SCREEN_WIDTH-20-60, 30)];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.textColor = SET_LABEL_TEXT_COLOR;
    nameLabel.font = [UIFont fontWithName:nil size:18];
    [self addSubview:nameLabel];
    
    UILabel *shareLabeLine = [[UILabel alloc]initWithFrame:CGRectMake(10, 79.5, CT_HEADER_SCREEN_WIDTH-20, 0.5)];
    shareLabeLine.backgroundColor = CT_HEADER_BRIGHTLINE_COLOR;
    [self addSubview:shareLabeLine];
    
}
#pragma mark - setter -

-(void)setPicUrl:(NSString *)picUrl
{
    [CTRequestUtil requestImgWithUrl:picUrl withCompletionSuccessHandler:^(UIImage *img) {
        headImageView.image = img;
    } withFailedHandler:^(NSError *error) {
        
    }];

}



@end